package com.nageoffer.shortlink.project.service.impl;

import com.nageoffer.shortlink.project.service.UrlTitleService;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * ClassName: UrlTitleServiceImpl
 * Package: com.nageoffer.shortlink.project.service.impl
 * Description:
 *
 * @Author Jetson
 * @Create 2024/10/22 19:54
 * @Version 1.0
 */

@Service
public class UrlTitleServiceImpl implements UrlTitleService {
//    @Override
//    public String getTitleByUrl(String url) {
//        return "";
//    }
    @SneakyThrows
    @Override
    public String getTitleByUrl(String url) {
        URL tagetUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) tagetUrl.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            Document document = Jsoup.connect(url).get();
            return document.title();
        }
        return "Erro while fetching title";

    }
}
